﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using V5_BLL;
using System.Data;
using V5_Common;
using V5_Model;
using System.Text;

namespace V5_EShop._Class.Data {
    public class V5ProductHelper {
        private static readonly V5ProductHelper m_Instance = new V5ProductHelper();
        /// <summary>
        /// 
        /// </summary>
        public static V5ProductHelper Instance {
            get { return m_Instance; }
        }
        BLLProduct bll = new BLLProduct();
        #region 产品操作
        /// <summary>
        /// 获取产品内容
        /// </summary>
        public DataRow GetDataRow(object oID) {
            string id = InputHelper.SetString(oID);
            DataTable dt = bll.GetList(" And ID=" + id).Tables[0];
            if (dt.Rows.Count > 0) {
                return dt.Rows[0];
            }
            else {
                return dt.NewRow();
            }
        }
        /// <summary>
        /// 获取产品列表列表 
        /// </summary>
        public DataRowCollection GetDataRowParams(string _params) {
            DataTable dt = new DataTable();
            string strWhere = string.Empty, strOrder = " ID Desc ";
            if (!string.IsNullOrEmpty(_params)) {
                string fileds = InputHelper.SetString(InputHelper.TagVal(_params, "fileds"));
                int channelID = InputHelper.SetNumber(InputHelper.TagVal(_params, "channelid"));
                int row = InputHelper.SetNumber(InputHelper.TagVal(_params, "row"));
                string where = InputHelper.SetString(InputHelper.TagVal(_params, "where"));
                string orderby = InputHelper.SetString(InputHelper.TagVal(_params, "orderby"));
                string classid = InputHelper.SetString(InputHelper.TagVal(_params, "classid"));
                string isSubClass = InputHelper.SetString(InputHelper.TagVal(_params, "subclass"));
                string attType = InputHelper.SetString(InputHelper.TagVal(_params, "atttype"));


                if (row < 1) {
                    row = 10;
                }
                if (InputHelper.SetNumber(classid) > 0) {
                    strWhere += " and ClassID=" + classid;
                }

                if (!string.IsNullOrEmpty(where)) {
                    strWhere += " and " + where;
                }

                if (!string.IsNullOrEmpty(orderby)) {
                    strOrder = orderby;
                }
                dt = bll.GetList(row, strWhere, strOrder).Tables[0];
            }
            return dt.Rows;
        }
        #endregion

        /// <summary>
        /// 多图
        /// </summary>
        public DataRowCollection GetProductMorePics(string ProductMorePic) {
            DataTable dt = new BLLProduct().Get_ProductMorePics(ProductMorePic);
            return dt.Rows;
        }
        /// <summary>
        /// 可选属性
        /// </summary>
        public DataRowCollection GetProductCustomChoice(object id) {
            DataTable customChoice = new DataTable();
            if (id == null) {
                return customChoice.Rows;
            }
            ModelProduct model = new BLLProduct().GetModel(InputHelper.SetNumber(id));
            customChoice = model.Choicetable;
            return customChoice.Rows;
        }

        /// <summary>
        /// 获取客户端
        /// </summary>
        public string Get_CustomChoice(int indexid, int filedid, string FieldValue) {
            ModelProductField modelproductfield = new BLLProductField().GetModel(filedid);
            if (modelproductfield.IsUseImg == 1)//判断是否用户可以选择
            {
                string fieldName = modelproductfield.FieldName;
                string fieldvalues = modelproductfield.FieldValues;
                string fieldpics = modelproductfield.FieldPics;

                string[] fieldvalueArr = fieldvalues.Split(new char[] { ',' });
                string[] filedpicArr = fieldpics.Split(new char[] { ',' });

                string[] s1 = FieldValue.Split(new char[] { ',' });

                StringBuilder sb = new StringBuilder();
                sb.Append("<li>");
                sb.Append(@"<span id=""C_custom_" + indexid + @""" class=""hong""><span class=""hui"">" + fieldName + @"：</span><span >" + s1[0] + "</span> </span>");
                sb.Append(@"<span class=""mianliao"">");


                for (int n = 0; n < s1.Length; n++) {
                    for (int i = 0; i < fieldvalueArr.Length; i++) {
                        for (int j = 0; j < filedpicArr.Length; j++) {
                            if (i == j && s1[n] == fieldvalueArr[i]) {
                                sb.Append(@"<img src=""UpLoadFiles/productFiled/" + filedpicArr[j] + @""" onclick=""changeCustom('custom_" + indexid + @"','" + fieldName + @"：" + s1[n] + @"')"">");
                            }
                        }
                    }
                }
                sb.Append(@"</span>");
                sb.Append(@"<div class=""clear""></div></li>");
                return sb.ToString();
            }
            return "";
        }

        /// <summary>
        /// 绑定其他字段属性
        /// </summary>
        public DataRowCollection GetProductPro(string ProductSingleFields) {
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("m1"));
            dt.Columns.Add(new DataColumn("m2"));
            if (ProductSingleFields.Trim().Length > 0) {
                string[] sarr1 = ProductSingleFields.Split(new string[] { "||" }, StringSplitOptions.None);
                foreach (string s1 in sarr1) {
                    string[] sarr2 = s1.Split(new string[] { ":" }, StringSplitOptions.None);
                    DataRow dr = dt.NewRow();
                    dr["m1"] = sarr2[0];
                    dr["m2"] = sarr2[1];
                    dt.Rows.Add(dr);
                }
            }
            return dt.Rows;
        }

        /// <summary>
        /// 产品评论
        /// </summary>
        public DataRowCollection GetProductComments(string strWhere) {
            DataTable dt = new BLLProductComment().GetList(0, strWhere, " ID Desc").Tables[0];
            return dt.Rows;
        }

        #region
        /// <summary>
        /// 获取产品单个属性
        /// </summary>
        /// <param name="oClassID"></param>
        /// <returns></returns>
        public DataRow GetProductClassSingle(object oClassID) {
            return V5.SQL.Query_By("*", "K_ProductClass", " ClassID=" + oClassID, "", 1).Rows[0];
        }
        /// <summary>
        /// 获取产品列表
        /// </summary>
        /// <param name="ClassID"></param>
        /// <param name="isSub"></param>
        /// <param name="what"></param>
        /// <param name="where"></param>
        /// <param name="orderby"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public DataRowCollection GetList(int ClassID, bool isSub, string what, string where, string orderby, int limit) {
            DataRowCollection drc = null;
            string ClassIDList = ClassID == 0 ? string.Empty : ClassID.ToString() + ",";
            if (isSub) {
                foreach (DataRow dr in GetClassList("ClassID", "ParentID=" + ClassID, string.Empty, 0)) {
                    ClassIDList += dr["ClassID"].ToString() + ",";
                }
            }
            if (ClassIDList.Length > 0) {
                ClassIDList = ClassIDList.Remove(ClassIDList.Length - 1);
            }
            if (!string.IsNullOrEmpty(where)) {
                where += " And ClassID in(" + ClassIDList + ") ";
            }
            else {
                where += " ClassID in(" + ClassIDList + ") ";
            }
            drc = V5.SQL.Query_By(what, "K_Product", where, orderby, limit).Rows;
            return drc;
        }
        #endregion
        #region 获取分类
        /// <summary>
        /// 获取分类信息
        /// </summary>
        /// <param name="what"></param>
        /// <param name="where"></param>
        /// <param name="orderby"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public DataRowCollection GetClassList(string what, string where, string orderby, int limit) {
            return V5.SQL.Query_By(what, "K_ProductClass", where, orderby, limit).Rows;
        }
        #endregion
    }
}